package com.gton.person.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: PersonalDesign
 * @description:登录时用户鉴权实体类
 * @author: GuoTong
 * @create: 2020-11-12 15:35
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User implements UserDetails {

    /*ID*/
    private Integer id;

    /*用户名*/
    private String userName;

    /*加密——密码*/
    private String passWord;

    /*密码明文*/
    private String pwdShow;

    /*一对多：一个用户多个角色：对应角色表*/
    private List<Role> roles;

    /*security框架的用户控制信息*/
    private Boolean locked;

    private Boolean enabled;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Role rolee : roles) {
            authorities.add(new SimpleGrantedAuthority(rolee.getName()));
        }
        authorities.forEach(System.out::println);
        return authorities;
    }

    @Override
    public String getPassword() {
        return passWord;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    //继承后必须返回为true
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
